/**
 * 判断是否深色系
 * hex: 色值
 */
export function isDarkColor(hex) {
	if (!/^#[0-9A-F]{6}$/i.test(hex)) {
		throw new Error('Invalid HEX color format');
	} else {
		let threshold = 128;
		let bigint = parseInt(hex.slice(1), 16);
		let r = (bigint >> 16) & 255;
		let g = (bigint >> 8) & 255;
		let b = bigint & 255;
		let brightness = 0.299 * r + 0.587 * g + 0.114 * b;
		return brightness < threshold;
	}
}

/**
 * 自动生成HEX颜色
 * number: 生成数量
 * type: 色系类型 dark / light / plain
 */
export function $genRandomHexColor(number, type = 'dark') {
	let colors = []; // 存放生成的颜色数组
	for (let i = 0; i < number; i++) {
		let hex = Math.floor(Math.random() * 16777216).toString(16);
		//生成ffffff以内16进制数
		while (hex.length < 6) {
			hex = "0" + hex; //while循环判断hex位数，少于6位前面加0凑够6位
		}
		let color = "#" + hex; // 将RGB值转换为字符串形式表示颜色
		if (!colors.includes(color)) {
			isDarkColor(color) ? colors.push(color) : i--; // 判断颜色是否重复
		} else {
			i--; // 若发现了重复颜色，则需要再次生成新的颜色
		}
	}
	return colors;
}